﻿using CommunityToolkit.Mvvm.Messaging;
using Koala.Pro.Common;
using Koala.Pro.Core;
using Koala.Pro.Core.Models;
using Koala.Pro.Models;
using Koala.Pro.Services;
using Koala.Pro.ViewModels;
using System.Collections.ObjectModel;

namespace Koala.Pro.Helpers
{
    public static class MainModelHelper
    {
        public static void SetCanBack(bool canBack = true)
        {
            MainWindowViewModel service = App.GetService<MainWindowViewModel>();
            if (service != null)
            {
                service.CanBack = canBack;
            }
        }

        public static void SetIsDirty(bool isDirty = true)
        {
            MainWindowViewModel service = App.GetService<MainWindowViewModel>();
            if (service != null)
            {
                service.IsDirty = isDirty;
            }
        }

        public static void SetBreadcrumbItems(List<string> items)
        {
            MainWindowViewModel service = App.GetService<MainWindowViewModel>();
            if (service != null && items != null)
            {
                service.BreadcrumbItems = new ObservableCollection<string>(items);
            }
        }

        public static async Task SetLoginInfo(UserInfo userInfo)
        {
            App.ViewModel.UserInfo = userInfo;
            var roleList = await BLLFactory<IRoleService>.Instance.GetRolesByUser(userInfo.Id);
            App.ViewModel.RoleList = roleList;
          
            string typeId = App.ViewModel.SystemType;
            var funcList = await BLLFactory<IFunctionService>.Instance.GetFunctionsByUser(userInfo.Id, typeId);
            App.ViewModel.FuncList = funcList;

            App.ViewModel.IsUserAdmin = roleList.Any((RoleInfo r) => r.Name == "超级管理员" || r.Name == "系统管理员");
            App.ViewModel.IsSuperAdmin = roleList.Any((RoleInfo r) => r.Name == "超级管理员");
            LoginUserInfo info = ConvertToLoginUser(userInfo, roleList);
            BLLFactory<IApiUserSession>.Instance.SetInfo(info);
            WeakReferenceMessenger.Default.Send(new UserLoginMessage(userInfo));

            await Task.CompletedTask;
        }

        public static LoginUserInfo ConvertToLoginUser(UserInfo info, List<RoleInfo> roleList = null)
        {
            LoginUserInfo loginUserInfo = new LoginUserInfo();
            loginUserInfo.Id = info.Id.ToString();
            loginUserInfo.Name = info.Name;
            loginUserInfo.FullName = info.FullName;
            loginUserInfo.IdentityCard = info.IdentityCard;
            loginUserInfo.MobilePhone = info.MobilePhone;
            loginUserInfo.QQ = info.QQ;
            loginUserInfo.Email = info.Email;
            loginUserInfo.Gender = info.Gender;
            loginUserInfo.DeptId = info.Dept_ID;
            loginUserInfo.DeptName = info.DeptName;
            loginUserInfo.CompanyId = info.Company_ID;
            loginUserInfo.CompanyName = info.CompanyName;
            List<string> macAddress = HardwareInfoHelper.GetMacAddress();
            loginUserInfo.MacAddr = ((macAddress.Count > 0) ? macAddress[0] : null);
            loginUserInfo.IP = NetworkUtil.GetIPAddress();
            if (roleList != null)
            {
                loginUserInfo.RoleIdList = new List<string>();
                loginUserInfo.RoleNameList = new List<string>();
                foreach (RoleInfo role in roleList)
                {
                    loginUserInfo.RoleIdList.Add(role.Id.ToString());
                    loginUserInfo.RoleNameList.Add(role.Name);
                }
                if (roleList.Any((RoleInfo s) => s.Name.Equals("超级管理员")))
                {
                    loginUserInfo.RoleType = "超级管理员";
                }
                else
                {
                    bool flag = roleList.Any((RoleInfo s) => s.Name.Equals("系统管理员"));
                    loginUserInfo.RoleType = (flag ? "系统管理员" : "其他");
                }
            }
            return loginUserInfo;
        }
    }
}
